package com.graden.background.controller.sysuser;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.graden.background.common.conf.aop.operation.OperationAnnotation;
import com.graden.background.common.conf.aop.sso.AopResourceUserInfoBean;
import com.graden.background.common.enums.OperationEnum;
import com.graden.background.common.enums.ResultEnum;
import com.graden.background.common.utils.apiresult.AbstractApiResult;
import com.graden.background.controller.sysuser.vo.*;
import com.graden.background.service.sysuser.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 系统用户（包含用户登录） 控制器
 * </p>
 *
 * @author zjf
 * @since 2019-03-21
 */
@RestController
@RequestMapping("/user")
@Api(tags = {"系统用户（包含用户登录）"})
public class SysUserController {

	@Resource
	ApplicationContext applicationContext;

	@Resource
	ISysUserService iSysUserService;


	@PostMapping("/login")
	@ApiOperation(value = "用户登录接口")
	@OperationAnnotation(operation = OperationEnum.LOGIN_COMMON)
	public AbstractApiResult loginUser(@RequestBody UserLoginVO userLoginVO) {
		return AbstractApiResult.success(iSysUserService.checkLogin(userLoginVO));
	}

	@GetMapping("/logout")
	@ApiOperation(value = "注销登录接口")
	@OperationAnnotation(operation = OperationEnum.LOGIN_OUT)
	public AbstractApiResult logoutUser() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String token = request.getHeader("token");
		return AbstractApiResult.success(iSysUserService.logout(token));
	}

	@GetMapping("/get")
	@ApiOperation(value = "获取当前登录人信息接口")
	//@OperationAnnotation(operation = OperationEnum.SYSTEM_USER_INFO)
	public AbstractApiResult getUser() {

		AopResourceUserInfoBean aopResourceUserInfoBean = applicationContext.getBean(AopResourceUserInfoBean.class);

		return AbstractApiResult.success(JSONObject.toJSON(aopResourceUserInfoBean));
	}

	@PostMapping("/page/{pageNo}/{pageSize}")
	@ApiOperation(value = "查询用户列表接口")
	@OperationAnnotation(operation = OperationEnum.SYSTEM_USER_SEE)
	public AbstractApiResult getPage(@PathVariable int pageNo, @PathVariable int pageSize, @RequestBody UserSelectVO userSelectVO) {

		PageInfo iPage = iSysUserService.getAllByPage(pageNo, pageSize, userSelectVO);

		return AbstractApiResult.success(iPage);
	}

	@PostMapping("/add")
	@ApiOperation(value = "用户添加接口")
	@OperationAnnotation(operation = OperationEnum.SYSTEM_USER_ADD)
	public AbstractApiResult addUser(@RequestBody UserAddVO userAddVO) {

		iSysUserService.addUser(userAddVO);

		return AbstractApiResult.success(ResultEnum.SUCCESS.getValue());
	}

	@PostMapping("/role/update")
	@ApiOperation(value = "用户角色修改接口")
	@OperationAnnotation(operation = OperationEnum.SYSTEM_USER_ROLE)
	public AbstractApiResult updateUser(@RequestBody UserRoleUpdateVO userUpdateVO) {

		iSysUserService.updateUser(userUpdateVO);

		return AbstractApiResult.success(ResultEnum.SUCCESS.getValue());
	}

	@PostMapping("/pwd/change")
	@ApiOperation(value = "用户密码修改接口")
	@OperationAnnotation(operation = OperationEnum.SYSTEM_USER_PASSWORD)
	public AbstractApiResult updateUserPassword(@RequestBody UserPwdUpdateVO userPwdUpdateVO) {

		iSysUserService.updatePassword(userPwdUpdateVO);

		return AbstractApiResult.success(ResultEnum.SUCCESS.getValue());
	}

	@PostMapping("/activate")
	@ApiOperation(value = "用户激活接口")
	@OperationAnnotation(operation = OperationEnum.SYSTEM_USER_ACTIVATE)
	public AbstractApiResult activateUser(@RequestBody UserActivateVO userActivateVO) {

		iSysUserService.activateUser(userActivateVO);

		return AbstractApiResult.success(ResultEnum.SUCCESS.getValue());
	}

	@DeleteMapping("/delete/{id}")
	@ApiOperation(value = "用户删除接口")
	@OperationAnnotation(operation = OperationEnum.SYSTEM_USER_DELETE)
	public AbstractApiResult deleteUser(@PathVariable int id) {

		iSysUserService.delete(id);

		return AbstractApiResult.success(ResultEnum.SUCCESS.getValue());
	}
}
